package com.rongc.client.freight.business.supply.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.DateUtil;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.ApiUrl;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.business.carrier.model.ConveyanceBean;

import java.util.List;

/**
 * Created by ccqx on 2017/6/28.
 */

public class RecyclerSupplyConveyanceAdapter extends RecyclerBaseAdapter<ConveyanceBean>{


    public RecyclerSupplyConveyanceAdapter(Context mContext, List<ConveyanceBean> mDatas) {
        super(mContext, mDatas);
    }

    @Override
    protected void onBind(final BaseRecyclerViewHolder holder, int position, ConveyanceBean data) {
        holder.getImageView(R.id.tv_phone).setVisibility(View.VISIBLE);
        holder.setText(R.id.tv_departure,data.getStart_ssq());
        holder.setText(R.id.tv_destination,data.getEnd_ssq());
        holder.setText(R.id.tv_distance, String.format(mContext.getResources().getString(R.string.supply_list_distance),
                data.getDistance()));
        holder.setText(R.id.tv_create_date, DateUtil.formatDateStr2Desc(data.getLoadTime(),DateUtil.dateFormatMD));
        holder.setText(R.id.tv_date,data.getLoadTime());
        holder.setText(R.id.tv_model,StringUtils.getNewModel(mContext,data.getCarLength(),data.getCarLoad(),data.getDulk(),data.getCarModels()));

        holder.getView(R.id.tv_phone).setTag(data.getPhone());
        holder.getView(R.id.tv_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = (String)v.getTag();
                ActivityUtils.callTo(mContext,number);
            }
        });

        Glide.with(UniApplication.getInstance())
                .load(ApiUrl.getImgUrl(data.getImage()))
                .asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.universal_head_default)
                .into(new BitmapImageViewTarget(holder.getImageView(R.id.iv_head)) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable roundedBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        roundedBitmapDrawable.setCircular(true);
//                                        roundedBitmapDrawable.setCornerRadius(20); //设置圆角半径（根据实际需求）
                        roundedBitmapDrawable.setAntiAlias(true); //设置反走样
                        getView().setImageDrawable(roundedBitmapDrawable);
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.recycler_supply_conveyance_item;
    }
}
